import java.util.*;


public class Environment {
	private int score;
	private int width;
	private int height;
	private Drawer d;
	private int tBullets=1;
	private int hitBullets=0;
	private long startTime = System.currentTimeMillis();
	private ArrayList<Asteroid> aList = new ArrayList<Asteroid>();
	private ArrayList<Bullet> bList = new ArrayList<Bullet>();
	private ArrayList<Turret> tList = new ArrayList<Turret>();
	private int health=100;
	
	public Environment(int width,int height){
		this.width=width;
		this.height=height;
		tList.add(new Turret(100,450,this));
		tList.add(new Turret(200,450,this));
		tList.add(new Turret(300,450,this));
		tList.add(new Turret(400,450,this));
		tList.add(new Turret(500,450,this));
		tList.add(new Turret(600,450,this));
	}

	public void setDrawer(Drawer d){
		this.d=d;
	}

	public void update(){
		if(health<=0)
		{
			d.setUpgrading(true);
			setHealth(10000);
			setaList(new ArrayList<Asteroid>());
			setbList(new ArrayList<Bullet>());
			
		}
		Random r = new Random();
		long timediff = (System.currentTimeMillis()-startTime)/1000;
		if(aList.size()<timediff)
		{
			for(int i=0;i<timediff+2;i++)
			{
				if(r.nextInt(10000)>9995)
				{
					aList.add(new Asteroid(r.nextInt(26)+4,r.nextInt(580)+30,0,0,1,this));
				}
			}
		}
		for(int i=0;i<aList.size();i++){
			Asteroid o = aList.get(i);
			o.move();
			if(o.getM()<=9){
				int score = getScore();
				score+= o.om;
				setScore(score);
				aList.remove(o);
				i--;
			}
		}
		for(int i=0;i<tList.size();i++){
			Turret o= tList.get(i);
			o.act(d.getMouseX(), d.getMouseY());
		}
		for(int i=0;i<bList.size();i++){
			Bullet o= bList.get(i);
			o.act();
		}
	}

	public void clear(){
		startTime = System.currentTimeMillis();
		aList = new ArrayList<Asteroid>();
		bList = new ArrayList<Bullet>();
		tList = new ArrayList<Turret>();
	}









	//Getters and Setters

	/**
	 * @return the width
	 */
	public int getWidth() {
		return width;
	}
	/**
	 * @param width the width to set
	 */
	public void setWidth(int width) {
		this.width = width;
	}



	/**
	 * @return the height
	 */
	public int getHeight() {
		return height;
	}
	/**
	 * @param height the height to set
	 */
	public void setHeight(int height) {
		this.height = height;
	}



	/**
	 * @return the Asteroid List
	 */
	public ArrayList<Asteroid> getaList() {
		return aList;
	}
	/**
	 * @param aList the Asteroid List to set
	 */
	public void setaList(ArrayList<Asteroid> aList) {
		this.aList = aList;
	}



	/**
	 * @return the Bullet List
	 */
	public ArrayList<Bullet> getbList() {
		return bList;
	}
	/**
	 * @param bList the Bullet List to set
	 */
	public void setbList(ArrayList<Bullet> bList) {
		this.bList = bList;
	}



	/**
	 * @return the Turret List
	 */
	public ArrayList<Turret> gettList() {
		return tList;
	}
	/**
	 * @param tList the Turret List to set
	 */
	public void settList(ArrayList<Turret> tList) {
		this.tList = tList;
	}

	/**
	 * @return the d
	 */
	public Drawer getD() {
		return d;
	}

	/**
	 * @param d the d to set
	 */
	public void setD(Drawer d) {
		this.d = d;
	}
	
	/**
	 * @return the score
	 */
	public int getScore() {
		return score;
	}
	/**
	 * @param score the score to set
	 */
	public void setScore(int score) {
		this.score = score;
	}

	/**
	 * @return the hitBullets
	 */
	public int getHitBullets() {
		return hitBullets;
	}

	/**
	 * @param hitBullets the hitBullets to set
	 */
	public void setHitBullets(int hitBullets) {
		this.hitBullets = hitBullets;
	}

	/**
	 * @return the tBulltes
	 */
	public int gettBullets() {
		return tBullets;
	}

	/**
	 * @param tBulltes the tBulltes to set
	 */
	public void settBullets(int tBullets) {
		this.tBullets = tBullets;
	}

	/**
	 * @return the health
	 */
	public int getHealth() {
		return health;
	}

	/**
	 * @param health the health to set
	 */
	public void setHealth(int health) {
		this.health = health;
	}

	/**
	 * @return the startTime
	 */
	public long getStartTime() {
		return startTime;
	}

	/**
	 * @param startTime the startTime to set
	 */
	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}

}
